home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 18 / fpc103.zip / MULTASK.SEQ < prev    next >
Text File  |  1988-06-30  |  6KB  |  157 lines

  1. \ MULTASK.SEQ   Multi tasking code for Forth.
  2.  
  3. PREFIX
  4.  
  5. ONLY FORTH ALSO DEFINITIONS
  6.  
  7. comment:
  8.  
  9.   The MultiTasker is loaded as an application on top of the
  10. regular Forth System.  There is support for it in the nucleus
  11. in the form of USER variables and PAUSEs inserted inside of
  12. KEY EMIT and BLOCK.  The Forth multitasking scheme is
  13. co-operative instead of interruptive.  All IO operations cause
  14. a PAUSE to occur, and the multitasking loop looks around at
  15. all of the current task for something to do.
  16.  
  17.   Modified by Tom Zimmer to work with F-PC, which needs to save the ES
  18. register along with IP. - 2/29/88 -
  19.  
  20. NOTICE !!       This multi tasking DOES NOT SUPPORT MULTIPLE USERS !!!
  21.  
  22.   There are a significant number of things what will have to be changed
  23. in this Forth system to support multi user, like the editor would have
  24. to be re-written, ect.  So I am not supporting multi user, only
  25. background processing.
  26.  
  27.   Background processing MAY NOT do any compiling, compile, like VARIABLE,
  28. or CREATE, ect. at *** RUNTIME ***.                     06/06/88 13:58
  29.  
  30.   YDP and XDP are NOT user variables.
  31.  
  32. comment;
  33.  
  34. CODE (PAUSE)    ( -- )
  35.                 PUSH ES         \ Push ES, IP, and RP
  36.                 PUSH IP
  37.                 PUSH RP
  38.                 MOV BX, UP      \ make BX point to user area
  39.                 MOV 0 [BX], SP  \ save SP in user area offset 0
  40.                 ADD BX, # 4     \ adjust BX to point to LINK
  41.                 ADD BX, 0 [BX]  \ Add value in LINK to BX, pointing it to
  42.                                 \ next tasks user area offset 0
  43.                 ADD BX, # 2     \ bump BX 2 to point to ENTRY
  44.                 JMP BX          \ jump to next tasks entry point
  45.                 END-CODE
  46.  
  47. CODE RESTART    ( -- )
  48.                 POP BX          \ pop address of where we came from
  49.                 MOV AX, # -4
  50.                 ADD BX, AX      \ adjust to beginning of user area
  51.                 MOV UP BX       \ set UP to point to begin of user area
  52.                 POP AX          \ pop off old PC
  53.                 POPF            \ restore status register
  54.                 MOV SP, 0 [BX]  \ restore SP (stack pointer)
  55.                 POP RP          \ restore RP, IP, and ES
  56.                 POP IP
  57.                 POP ES
  58.                 NEXT            END-CODE
  59.  
  60. HEX   E9 CONSTANT INT#
  61.  
  62. : LOCAL         ( base addr -- addr' )   UP @ -   +   ;
  63. : @LINK         ( -- addr ) LINK DUP @ +   2+   ;
  64. : !LINK         ( addr -- ) LINK 2+ -   LINK !   ;
  65. : SLEEP         ( addr -- ) E990 SWAP ENTRY LOCAL !   ;
  66. : WAKE          ( addr -- ) E9CD SWAP ENTRY LOCAL !   ;
  67. : STOP          ( -- )      UP @ SLEEP   PAUSE   ;
  68. : SINGLE        ( -- )
  69.                 E9    ['] PAUSE C!              \ set JMP in PAUSE
  70.                 >NEXT ['] PAUSE >BODY -         \ get offset to NEXT
  71.                       ['] PAUSE 1+ ! ;          \ store in PAUSE + 1
  72.  
  73. HEX
  74.  
  75. CODE MULTI      ( -- )
  76.                 MOV ' PAUSE # E9
  77.                 MOV BX, # ' (PAUSE) ' PAUSE 3 + -       \ relative I hope!
  78.                 MOV ' PAUSE 1+ BX
  79.                 MOV BX, # ' RESTART
  80.                 MOV AX, DS
  81.                 PUSH AX
  82.                 SUB AX, AX      MOV DS, AX
  83.                 MOV AX, CS      MOV INT# 4 * 2+ AX
  84.                                 MOV INT# 4 *    BX
  85.                 POP AX
  86.                 MOV DS, AX      NEXT
  87.                 END-CODE
  88.  
  89. UP @ WAKE   ENTRY !LINK
  90.  
  91. DECIMAL
  92.  
  93. : TASK:         ( size -- )
  94.                 CREATE   TOS HERE #USER @ CMOVE   ( Copy the USER Area )
  95.                 @LINK  UP @ -ROT  HERE UP !  !LINK ( I point where he did)
  96.                 DUP HERE +   DUP RP0 !   100 - SP0 !  SWAP UP !
  97.                 HERE ENTRY LOCAL !LINK    ( He points to me)
  98.                 HERE #USER @ +  HERE DP LOCAL !
  99.                 HERE SLEEP   ALLOT   ;
  100.  
  101. : SET-TASK      ( ES ip task -- )       \ NOTE: both ES and IP are passed
  102.                                         \ to SET-TASK.
  103.                 >R SWAP R>
  104.                 DUP SP0 LOCAL @                 \ Top of Stack
  105.                 2- ROT OVER !                   \ Initial ES
  106.                 2- ROT OVER !                   \ Initial IP
  107.                 2- OVER RP0 LOCAL @ OVER !      \ Initial RP
  108.                 SWAP TOS LOCAL !  ;
  109.  
  110. : ACTIVATE      ( task -- )
  111.                 DUP
  112.                 R> R> SWAP ROT SET-TASK   WAKE  ;
  113.  
  114. : BACKGROUND:   ( -- )
  115.                 400 TASK:
  116.                 XHERE @LINK 2-          \ get address of new task, note that
  117.                                         \ XHERE returns SEGMENT and OFFSET,
  118.                                         \ which is used by SET-TASK
  119.                 SET-TASK  !CSP  ]  ;
  120.  
  121. comment:
  122.  
  123.   Here is a sample of how to create a background task that will do a
  124. listing of the current file, with the hypothetical word FUNCTION1.  The
  125. task MYTASK is created with the default function FUNCTION1 assigned to it.
  126. Next we define a word MYTASK-THIS, what changes the function assigned to
  127. MYTASK to perform FUNCTION2.  This allows us to change the function a
  128. task performs without having to define a new task. In each case, the
  129. task is stopped after its assigned FUNCTION is performed.
  130.  
  131.  
  132.         background: mytask  function1 stop ;
  133.  
  134.         : mytask-this   mytask activate   function2 stop  ;
  135.  
  136.  
  137.   This next example defines a variable, and a routine which increments
  138. the variable in the background.  Notice that the program is an infinite
  139. loop, and will only stop when put to sleep, or when multi tasking is
  140. turned off with SINGLE.  This example will actually work, you might try
  141. typing it into a file and loading it.
  142.  
  143.  
  144. variable counts
  145. background: counter   begin pause 1 counts +! again  ;
  146.  
  147. MULTI COUNTER WAKE              \ start up the COUNTER task
  148.  
  149. \ COUNTER SLEEP                 \ put the COUNTER task to sleep
  150. \ SINGLE                        \ disable multi tasking
  151.  
  152. comment;
  153.  
  154. ONLY FORTH ALSO DEFINITIONS
  155.  
  156.  
  157.